package com.zztl.infiniteBand.ui.activity

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.TextView
import butterknife.OnClick
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.net.requestBean.GetVerifyRequest
import com.zztl.infiniteBand.net.requestBean.RegisterRequest
import com.zztl.infiniteBand.presenter.AmendPwdPresenter
import com.zztl.infiniteBand.storage.BillPreference
import com.zztl.infiniteBand.storage.Constans
import com.zztl.infiniteBand.storage.PreferenceKeys
import com.zztl.infiniteBand.ui.view.CommonItemView
import com.zztl.infiniteBand.ui.view.CommonItemView2
import com.zztl.infiniteBand.ui.view.mvpview.RegisterView
import com.zztl.infiniteBand.util.LogUtils
import com.zztl.infiniteBand.util.toast
import kotlinx.android.synthetic.main.activity_forget_or_reset_pwd.*


class ForgetOrResetPwdActivity : BaseMvpActivity<RegisterView, AmendPwdPresenter>(), RegisterView, CommonItemView.OnItemRightClickListener, TextWatcher {


    lateinit var mAmendPwdPresenter: AmendPwdPresenter
    lateinit var mRightTv: TextView

    private var isLook = false
    private var isLook2 = false


    override fun setLayoutId(): Int = R.layout.activity_forget_or_reset_pwd


    override fun initBase() {
        val phone = BillPreference.getApp(PreferenceKeys.DEFAULT_PHONE.name, "") as String
        val mFrom = intent.getStringExtra(Constans.ACTIVITY_FROM)
        if ("login" == mFrom) {
            tv_register.text = getString(R.string.忘记密码)
            item_phone!!.middleEdit!!.isEnabled = true
        } else if ("reset" == mFrom) {
            tv_register.text = getString(R.string.修改密码)
            item_phone!!.middleEdit!!.isEnabled = false
            item_phone.leftImgTv.setCompoundDrawables(null, null, null, null)
        }
        item_phone.setMiddleEditTextStr(phone)
        item_code.setItemRightClickListener(this)
        mRightTv = item_code.rightTv
        item_phone.middleEdit.addTextChangedListener(this)
        item_code.middleEdit.addTextChangedListener(this)
        item_pwd.middleEdit.addTextChangedListener(this)
        item_repet_pwd.middleEdit.addTextChangedListener(this)
        item_pwd.setRightImg(R.mipmap.biyanjing)
        item_repet_pwd.setRightImg(R.mipmap.biyanjing)


        item_phone.leftImgTv.apply {
            text = "+${BillPreference.getApp(PreferenceKeys.COUNTYRCODE.name, "86") as String}"
            setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    if ("login" == mFrom) {
                        val intent = Intent(this@ForgetOrResetPwdActivity, CountryCodeActivity::class.java)
                        showActivityForResult(intent, Constans.COUNTRY_CODE)
                    }
                }
            })
        }

        item_pwd.setItemRightClickListener(object : CommonItemView2.OnItemRightClickListener {
            override fun onClickItemRight(viewID: Int) {
                isLook = !isLook.apply {
                    LogUtils.i("swl", "isLook==$isLook")
                    if (this) {
                        item_pwd.middleEdit.transformationMethod= null
                        item_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
                        item_pwd.setRightImg(R.mipmap.zhengyanjing)
                    } else {
                        item_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
                        item_pwd.middleEdit.transformationMethod = PasswordTransformationMethod.getInstance()
                        item_pwd.setRightImg(R.mipmap.biyanjing)
                    }
                }
            }
        })

        item_repet_pwd.setItemRightClickListener(object : CommonItemView2.OnItemRightClickListener {
            override fun onClickItemRight(viewID: Int) {
                isLook2 = !isLook2.apply {
                    if (this) {
                        item_repet_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT
                        item_repet_pwd.setRightImg(R.mipmap.zhengyanjing)
                    } else {
                        item_repet_pwd.middleEdit.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                        item_repet_pwd.setRightImg(R.mipmap.biyanjing)
                    }
                }
            }
        })

        item_code.middleEdit.run {
            inputType=InputType.TYPE_TEXT_FLAG_MULTI_LINE
            setHorizontallyScrolling(false)
            setSingleLine(false)
        }

        item_pwd.middleEdit.run {
            inputType= InputType.TYPE_TEXT_FLAG_MULTI_LINE
            setSingleLine(false)
            setHorizontallyScrolling(false)
            transformationMethod = PasswordTransformationMethod.getInstance()
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        data?.let {
            if (Constans.COUNTRY_CODE == requestCode && resultCode == Activity.RESULT_OK) {
                item_phone.leftImgTv.text = "+${data.getStringExtra(Constans.COUNTRY_CODE_STR)}"
            }
        }
    }

    override fun createPresenter(): AmendPwdPresenter {
        mAmendPwdPresenter = AmendPwdPresenter()
        return mAmendPwdPresenter
    }

    override fun afterTextChanged(s: Editable?) {
        s?.let {
            btn_reset_pwd.isEnabled = item_phone.middleEditStr.isNotEmpty() && item_code.middleEditStr.length == 4
                    && item_pwd.middleEditStr.length >= 6 && item_repet_pwd.middleEditStr.equals(item_pwd.middleEditStr)
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }


    @OnClick(R.id.btn_reset_pwd, R.id.btn_return)
    fun click(view: View) {
        when (view.id) {
            R.id.btn_reset_pwd -> {
                if (mAmendPwdPresenter.check(item_phone.middleEditStr, item_pwd.middleEditStr, item_repet_pwd.middleEditStr, item_code.middleEditStr)) {
                    mAmendPwdPresenter.amendPwd(RegisterRequest(item_phone.middleEditStr, item_pwd.middleEditStr, item_code.middleEditStr, item_phone.leftImgTv.text.toString()), this)
                }
            }
            R.id.btn_return -> finish()
        }
    }

    override fun registerSuccess() {
        BillPreference.getApp(PreferenceKeys.DEFAULT_PHONE.name, item_phone.middleEditStr)
        skipActivity(this, LoginActivity::class.java)
    }

    override fun registerFail() {

    }

    override fun getCodeFail() {
        mAmendPwdPresenter.resetSmsView()
    }

    override fun setDisableView(count: Int) {
        mRightTv.isClickable = false
        mRightTv.text = "(" + count + "S)" + getString(R.string.重新发送)
        mRightTv.setTextColor(resources.getColor(R.color.color_ACA593))
    }

    override fun setUseableView() {
        mRightTv.isClickable = true
        mRightTv.text = getString(R.string.发送验证码)
        mRightTv.setTextColor(resources.getColor(R.color.color_007aff))
    }

    override fun getCodeSuccess() {

    }


    override fun onClickItemRight(viewID: Int) {
        if (TextUtils.isEmpty(item_phone.middleEditStr)) {
//            ToastUtils.show(this, getString(R.string.请输入手机号))
            toast(getString(R.string.请输入手机号))
        } else {
            mAmendPwdPresenter.startCountDown(this)
            mAmendPwdPresenter.getCode(GetVerifyRequest(item_phone.middleEditStr, "4", item_phone.leftImgTv.text.toString()), this)
        }
    }

    override fun onDestroy() {
        mAmendPwdPresenter.destoryTimer()
        super.onDestroy()
    }
}
